package com.zhj.interview;

/**
 * @author zhj
 * @create 2023-10-07-09:13
 */
public class Test21 {

    private static final Object lock = new Object();
    private static boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberPrinter = new Thread(new NumberPrinter());
        Thread letterPrinter = new Thread(new LetterPrinter());
        System.out.println("numberPrinter:" + numberPrinter.getState());
        System.out.println("letterPrinter:" + letterPrinter.getState());
        numberPrinter.start();
        letterPrinter.start();
        for (int i = 0; i < 9; i++) {
            try {
                System.out.println("numberPrinter:" + numberPrinter.getState());
                System.out.println("letterPrinter:" + letterPrinter.getState());
                Thread.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class NumberPrinter implements Runnable {

        @Override
        public void run() {
            synchronized (lock) {
                try {
                    try {
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    for (int i = 1; i <= 3; i++) {
                        while (!printNumber) {
                            lock.wait();
                            try {
                                Thread.sleep(1);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.print(i);
                        printNumber = false;
                        lock.notify();
                        try {
                            Thread.sleep(1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class LetterPrinter implements Runnable {

        @Override
        public void run() {
            synchronized (lock) {
                try {
                    try {
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    while (!printNumber) {
                        for (char c = 'A'; c <= 'C'; c++) {
                            while (printNumber) {
                                lock.wait();
                                try {
                                    Thread.sleep(1);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            System.out.print(c);
                            printNumber = true;
                            lock.notify();
                            try {
                                Thread.sleep(1);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
